package achao.gallary.com.normalgallary;

import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PhotoListActivity extends AppCompatActivity {
    public RecyclerView rv_photo_list;
    private List<ImageBean> photos = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_list);
        rv_photo_list = (RecyclerView) findViewById(R.id.rv_photo_list);
        final GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        rv_photo_list.setLayoutManager(gridLayoutManager);
        initPhotos();

        CommonAdapter<ImageBean> commonAdapter = new CommonAdapter<ImageBean>(this, R.layout.item_photo_list, photos) {
            @Override
            protected void convert(ViewHolder holder, ImageBean imageBean, final int position) {
                final ImageView imageView = holder.getView(R.id.iv_pic);
                /// imageView.setImageResource(R.mipmap.ic_launcher);
                String baseUrl = "http://192.168.2.222/api/v1/storages/%s/%d";
                String url = String.format(baseUrl, imageBean.getImageUrl(), 10);
                Glide.with(PhotoListActivity.this)
                        .load(url)
                        .into(imageView);

            }
        };

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                String content = "position-->" + position
                        + "\nrecyclerview-->" + rv_photo_list.getChildCount()
                        + "\nFirstVisible" + gridLayoutManager.findFirstVisibleItemPosition()
                        + "\nFirstCompletely" + gridLayoutManager.findFirstCompletelyVisibleItemPosition()
                        + "\nLastVisibleItem" + gridLayoutManager.findLastVisibleItemPosition()
                        + "\nLastCompletelyVisible" + gridLayoutManager.findLastCompletelyVisibleItemPosition();

                Toast.makeText(PhotoListActivity.this, content, Toast.LENGTH_SHORT).show();

                //rv_photo_list.scrollToPosition(19);


                ArrayList<AnimationRect> animationRectArrayList
                        = new ArrayList<AnimationRect>();
                for (int i = 0; i < photos.size(); i++) {

                  /*  if (imageView.getVisibility() == View.VISIBLE) {
                        Log.i("haha", "imageView------>");*/
                    if (i < gridLayoutManager.findFirstCompletelyVisibleItemPosition()) {
                        // 顶部，无法全部看见的图片
                        AnimationRect rect=new AnimationRect();
                        animationRectArrayList.add(rect);
                    } else if (i > gridLayoutManager.findLastCompletelyVisibleItemPosition()) {
                        // 底部，无法完全看见的图片
                        AnimationRect rect=new AnimationRect();
                        animationRectArrayList.add(rect);
                    } else {
                        ImageView imageView = (ImageView) gridLayoutManager
                                .getChildAt(i-gridLayoutManager.findFirstVisibleItemPosition());
                        // 可以完全看见的图片
                        AnimationRect rect = AnimationRect.buildFromImageView(imageView);
                        animationRectArrayList.add(rect);
                    }

                    // }
                }
                Log.i("gridLayoutManager", gridLayoutManager.getChildCount() + "个ImageView");
                Log.i("animationRectArrayList", animationRectArrayList.toString());
                Intent intent = PhotoViewActivity
                        .newIntent(PhotoListActivity.this, (ArrayList<ImageBean>) photos, animationRectArrayList, position);
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        rv_photo_list.setAdapter(commonAdapter);
    }

    private void initPhotos() {
      /*  String[] imgs = {"http://f.hiphotos.baidu.com/image/pic/item/b151f8198618367a9f738e022a738bd4b21ce573.jpg"
                , "http://d.hiphotos.baidu.com/image/h%3D200/sign=3ab4f1c5c25c10383b7ec9c28213931c/e1fe9925bc315c60e74c9a3089b1cb1348547710.jpg"
                , "http://c.hiphotos.baidu.com/image/h%3D200/sign=2a7bb7e108f3d7ca13f63876c21ebe3c/a2cc7cd98d1001e98b58e4d0bc0e7bec55e7978d.jpg"
                , "http://f.hiphotos.baidu.com/image/h%3D200/sign=c226bdde0e55b31983f9857573ab8286/279759ee3d6d55fbdf1e87f969224f4a21a4dd11.jpg"
                , "http://h.hiphotos.baidu.com/image/h%3D200/sign=875443c1327adab422d01c43bbd5b36b/bf096b63f6246b60737e429deff81a4c500fa2e3.jpg"
                , "http://h.hiphotos.baidu.com/image/h%3D200/sign=410657b7d509b3def4bfe368fcbe6cd3/d1160924ab18972bc34ff015e2cd7b899f510af3.jpg"
                , "http://d.hiphotos.baidu.com/image/h%3D200/sign=88b6910832d12f2ed105a9607fc3d5ff/d0c8a786c9177f3e94530cd274cf3bc79e3d568e.jpg"
                , "http://f.hiphotos.baidu.com/image/pic/item/64380cd7912397ddcc4154305c82b2b7d0a287ce.jpg"};*/
        String[] imgs = new String[20];
        for (int i = 0; i < imgs.length; i++) {
            imgs[i] = 10 + i + "";
        }
        int i = 0;
        for (String string : imgs) {
            ImageBean imageBean = new ImageBean();
            imageBean.setImageUrl(string);
            imageBean.setPosition(i);
            i++;
            photos.add(imageBean);
        }
    }
}
